package jdklearning.net;

import java.io.UnsupportedEncodingException;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author shenenlu 2020年12月23日 下午15:55:18
 */
public class UriAndUrl {

    public static void main(String[] args) throws URISyntaxException,MalformedURLException{


    }

    /***
     * 1. URI统一资源标识符
     * URI(Uniform Resource Identify)，统一资源标识符，是一个用于标识某一互联网资源名称的字符串。
     *
     * URI的结构是 [scheme:]scheme-specific-part[#fragment]和[scheme:][//authority][path][?query][#fragment]
     * opaque表示URI是不是透明的，一个不透明的URI一般是不分层次的。
     */
    public static void testUri() throws URISyntaxException {
        String[] address = new String[]{
                "http://user:pwd@www.abc.com:8080/a/b/c?q=java#fragment",
                "tel:+123-4567-8910#frag"
        };

        for (String addr : address) {
            URI uri = new URI(addr);

            System.out.println("URL:        " + uri);
            System.out.println("Scheme:     " + uri.getScheme());
            if (uri.isOpaque()) {
                System.out.println("Specific:   " + uri.getSchemeSpecificPart());
            } else {
                System.out.println("UserInfo:   " + uri.getUserInfo());
                System.out.println("Authority:  " + uri.getAuthority());
                System.out.println("Host:       " + uri.getHost());
                System.out.println("Port:       " + uri.getPort());
                System.out.println("Path:       " + uri.getPath());
                System.out.println("Query:      " + uri.getQuery());
            }
            System.out.println("Fragment:   " + uri.getFragment());
            System.out.println();
        }
    }


    /***
     * 2. URL统一资源定位符
     * URL(Uniform Resource Location)，统一资源定位符，可以从互联网上得到的资源的位置和访问方法的一种简洁的表示，是互联网上标准资源的地址。URL主要由Protocol://Authority/File#ref组成。
     * 其中Authority包含UserInfo、Host和Port，File由Path和Query组成。
     */
    public static void testUrl() throws MalformedURLException {
        String[] address = new String[]{
                "http://user:pwd@www.abc.com:80/a/b/c?q=java#fragment",
                "ftp://www.abc.com:8080/"
        };

        for (String addr : address) {
            URL url = new URL(addr);

            System.out.println("URL:        " + url);
            System.out.println("Protocol:   " + url.getProtocol());
            System.out.println("UserInfo:   " + url.getUserInfo());
            System.out.println("Authority:  " + url.getAuthority());
            System.out.println("Host:       " + url.getHost());
            System.out.println("Port:       " + url.getPort());
            System.out.println("DefaultPort:" + url.getDefaultPort());
            System.out.println("Path:       " + url.getPath());
            System.out.println("File:       " + url.getFile());
            System.out.println("Query:      " + url.getQuery());
            System.out.println("Ref:        " + url.getRef());
            System.out.println();
        }

    }

    /**
     *3. URLEncoder和URLDecoder
     * 将非文本内容转换成"%xx"的形式。
     */
    public static void encodeAndDecode(){
        try {
            URLEncoder.encode("value1=123&&valu2=pwd","UTF-8"); // value1%3D123%26valu2%3Dpwd
            URLDecoder.decode("value1%3D123%26valu2%3Dpwd", "UTF-8"); // value1=123&valu2=pwd
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


}
